package com.net.request

import com.google.gson.Gson
import com.net.NetUtils
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/6/28
 */
class PostJsonRequestBuilder:BaseRequestBuilder() {
    override fun build(): NetUtils {
        val build = Request.Builder()
        //URL 参数
        build.url(url)
        //请求tag
        tag?.let {
            build.tag(tag)
        }
        //请求header
        headers?.let {
            build.headers(generateHeader(it))
        }
        //请求方式
        build.post(getPostBody(params))
        return NetUtils(build.build())
    }

    private fun getPostBody(params: MutableMap<String, String>?): RequestBody {
        val paramsJsonStr  = Gson().toJson(params)
        return paramsJsonStr.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
    }
}